﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GoShopping.Model;
using GoShopping.BLL;
using GoShopping.BLL.SysInfo;
using System.Collections.Generic;

public partial class Member_ModifyUserAddr : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Session["IsSubmit"] = false;
            ResponseCityOrCounty();
            SetProvinceDDL();
            BindRptAddr();
        }
    }

    private void BindRptAddr()
    {
        GoShopping.BLL.ProfileInfo.AddressListBean alb = new GoShopping.BLL.ProfileInfo.AddressListBean();
        RptAddress.DataSource = alb.GetUserAddressList(GetRelatedColumn.GetUserID(Context.User.Identity.Name));
        RptAddress.DataBind();
    }

    protected void RptAddress_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }
    private void AddNewAddress(GoShopping.Model.UserInfo ui, GoShopping.BLL.ProfileInfo.AddressListBean alb, GoShopping.Model.AddressList al)
    {
        al.UserID = Convert.ToInt32(ui.ID);
        al.AcceptPeople = txtFirstName.Text.Trim();
        al.ProvinceID = Convert.ToInt32(ProvinceSelect.Items[ProvinceSelect.SelectedIndex].Value);
        al.CityID = Convert.ToInt32(Request.Form["ctl00$ContentPlaceHolder1$CitySelect"]);
        al.CountyID = Convert.ToInt32(Request.Form["ctl00$ContentPlaceHolder1$CountySelect"]);
        al.DetailStreet = txtAddress1.Text.Trim();
        al.ZipCode = txtZip.Text.Trim();
        al.AcceptPeopleMobileTel = txtPhone.Text.Trim();
        al.AcceptPeopleTel = txtFixedPhone.Text.Trim();
        alb.AddOneAddressList(al);
        //Session["IsSubmit"] = true;
        BindRptAddr();
    }

    #region 级联菜单处理
    /// <summary>
    /// 返回DropDownList的数据,CityOrCounty
    /// </summary>
    private void ResponseCityOrCounty()
    {
        string str = "";
        if (Request.QueryString["provinceId"] != null)
        {
            Response.Expires = -1;
            Response.ContentType = "text/xml";
            str = GetDropDownList(Request.QueryString["ProvinceId"], "proId");
            Response.Write(str);
            Response.End();
        }
        else if (Request.QueryString["cityId"] != null)
        {
            Response.Expires = -1;
            Response.ContentType = "text/xml";
            str = GetDropDownList(Request.QueryString["cityId"], "cityId");
            Response.Write(str);
            Response.End();
        }
    }
    AreaControl ac = new GoShopping.BLL.SysInfo.AreaControl();
    /// <summary>
    /// 初始化级联菜单
    /// </summary>
    void SetProvinceDDL()
    {
        Province province = new Province();
        ProvinceSelect.DataTextField = "Name";
        ProvinceSelect.DataValueField = "ID";
        ProvinceSelect.DataSource = ac.GetAllProvince();
        ProvinceSelect.DataBind();

        CitySelect.DataTextField = "Name";
        CitySelect.DataValueField = "ID";
        if (ProvinceSelect.SelectedIndex >= 0)
        {
            CitySelect.DataSource = ac.GetProvinceCity(Convert.ToInt32(ProvinceSelect.Items[ProvinceSelect.SelectedIndex].Value));
            CitySelect.DataBind();
        }

        CountySelect.DataTextField = "Name";
        CountySelect.DataValueField = "ID";
        if (CitySelect.SelectedIndex >= 0)
        {
            CountySelect.DataSource = ac.GetCityCounty(Convert.ToInt32(CitySelect.Items[CitySelect.SelectedIndex].Value));
            CountySelect.DataBind();
        }
    }
    /// <summary>
    /// 处理页面返回的DropDownList数据以确定返回的数据
    /// </summary>
    /// <param name="id"></param>
    /// <param name="flag"></param>
    /// <returns></returns>
    private string GetDropDownList(string id, string flag)
    {
        string str = "";
        switch (flag)
        {
            case "proId":
                {
                    CitySelect.Items.Clear();
                    IList<City> iCity = ac.GetProvinceCity(Convert.ToInt32(id));
                    str += "<city>";
                    for (int i = 0; i < iCity.Count; i++)
                    {
                        str += "<cl>" + iCity[i].Name + "," + iCity[i].ID + "</cl>";
                    }
                    str += "</city>";
                } break;
            case "cityId":
                {
                    CountySelect.Items.Clear();
                    IList<County> iCounty = ac.GetCityCounty(Convert.ToInt32(id));
                    str += "<county>";
                    for (int i = 0; i < iCounty.Count; i++)
                    {
                        str += "<ct>" + iCounty[i].Name + "," + iCounty[i].ID + "</ct>";
                    }
                    str += "</county>";
                } break;
            default: break;
        }
        return str;
    }
    #endregion

    protected void btnAddAddr_Click(object sender, EventArgs e)
    {
        //if ((bool)Session["IsSubmit"])
        //{
        //    GoShopping.Utility.MessageBox.Show(this, "请不要重复提交!");
        //    return;
        //}
        GoShopping.BLL.UserBean ub = new GoShopping.BLL.UserBean();
        GoShopping.Model.UserInfo ui = ub.GetOneUser(Context.User.Identity.Name);
        GoShopping.BLL.ProfileInfo.AddressListBean alb = new GoShopping.BLL.ProfileInfo.AddressListBean();
        GoShopping.Model.AddressList al = new AddressList();
        try
        {
            AddNewAddress(ui, alb, al);
        }
        catch
        {
 
        }
    }

    protected void btnDelAddr_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in RptAddress.Items)
        {
            if (item.FindControl("rbAddress") != null)
            {
                //RadioButton rb = (RadioButton)item.FindControl("rbAddress");
                if (((RadioButton)(item.FindControl("rbAddress"))).Checked == true)
                {
                    GoShopping.BLL.ProfileInfo.AddressListBean alb = new GoShopping.BLL.ProfileInfo.AddressListBean();
                    alb.DelOneAddressList(Convert.ToInt32(((Label)(item.FindControl("lblID"))).Text));
                    BindRptAddr();
                }
            }
        }
    }
}
